package com.chery.media.library.widget.title

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.chery.media.library.R
import com.chery.media.library.extend.toColorInt

/**
 * @author MoYe_PanDeng
 * @ClassName: TitleBar
 * @Description: 标题栏
 * @date: 2021/5/15
 */
class TitleBar(private val mContext: Activity, isFinish: Boolean = true) {
    /**
     * 跟布局
     * 左侧/右侧图标和中间标题
     */
    private val commonHeadView: ConstraintLayout

    /**
     * 左侧图标
     */
    private val backImage: ImageView

    /**
     * 右侧图标
     */
    private val rightImage: ImageView

    /**
     * 中间标题
     */
    private val titleText: TextView

    /**
     * 右侧标题
     */
    private val rightText: TextView

    /**
     * 用于设置标题栏文字
     *
     * @param titleText 传入要设置的标题
     * @return
     */
    fun setTitleText(title: String?): TitleBar {
        if (title.isNullOrEmpty().not()) {
            titleText.text = title
        }
        return this
    }

    /**
     * 设置标题栏文字颜色
     *
     * @return
     */
    fun setTitleTextColor(color: Int): TitleBar {
        titleText.setTextColor(color)
        return this
    }

    /**
     * 设置标题栏右边的文字
     *
     * @return
     */
    fun setTitleRight(rightTitle: String?, color: Int = R.color.public_111111.toColorInt()): TitleBar {
        if (rightTitle.isNullOrEmpty().not()) {
            rightText.visibility = View.VISIBLE
            rightImage.visibility = View.GONE
            rightText.text = rightTitle
            rightText.setTextColor(color)
        }
        return this
    }

    /**
     * 用于设置标题栏左边要显示的图片
     *
     * @param resId 标题栏左边的图标的id，一般为返回图标
     * @return
     */
    fun setLeftIco(resId: Int): TitleBar {
        backImage.visibility = if (resId > 0) View.VISIBLE else View.GONE
        if (resId > 0) backImage.setImageResource(resId)
        return this
    }

    /**
     * 用于设置标题栏右边要显示的图片
     *
     * @param resId 标题栏右边的图标id
     * @return
     */
    fun setRightIco(resId: Int): TitleBar {
        rightImage.visibility = if (resId > 0) View.VISIBLE else View.GONE
        rightImage.setImageResource(resId)
        return this
    }

    /**
     * 用户设置 标题栏右侧的图标的背景drawable
     *
     * @param resId drawable的id
     * @return
     */
    fun setRightIconBgDr(resId: Int): TitleBar {
        rightImage.visibility = if (resId > 0) View.VISIBLE else View.GONE
        rightImage.setImageResource(resId)
        return this
    }

    /**
     * 用于设置标题栏左边图片的单击事件
     *
     * @param listener 传入的事件对象
     * @return
     */
    fun setLeftIcoListening(listener: View.OnClickListener?): TitleBar {
        if (backImage.visibility == View.VISIBLE) {
            backImage.setOnClickListener(listener)
        }
        return this
    }

    /**
     * 返回
     *
     * @param activity
     * @return
     */
    fun setLeftBack(activity: Activity?): TitleBar {
        if (activity != null) {
            backImage.setOnClickListener{
                hideInputFromWindow()
                activity.finish()
            }
        }
        return this
    }

    /**
     * 用于设置标题栏右边图片的单击事件
     *
     * @param listener 传入的事件对象
     * @return
     */
    fun setRightListening(listener: View.OnClickListener?): TitleBar {
        if (rightImage.visibility == View.VISIBLE) {
            rightImage.setOnClickListener(listener)
        }
        if (rightText.visibility == View.VISIBLE) {
            rightText.setOnClickListener(listener)
        }
        return this
    }

    /**
     * 隐藏左边的返回
     *
     * @return
     */
    fun hideLeftBack(): TitleBar {
        backImage.visibility = View.GONE
        return this
    }

    /**
     * 返回根布局
     *
     * @return
     */
    val titleBarView: View
        get() = commonHeadView

    /**
     * 隐藏键盘
     */
    fun hideInputFromWindow() {
        val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(mContext.window.decorView.windowToken, 0)
    }

    /**
     * 初始化 获取view
     */
    init {
        val view = mContext.findViewById<View>(R.id.common_bar)
        commonHeadView = view.findViewById(R.id.common_bar)
        titleText = view.findViewById(R.id.titleText)
        rightText = view.findViewById(R.id.rightText)
        backImage = view.findViewById(R.id.backImage)
        rightImage = view.findViewById(R.id.rightImage)
        if (isFinish) setLeftBack(mContext)
    }
}